class Util{
	request(param){
		return new Promise((resolve,reject) => {
			$.ajax({
				type      : param.type     || 'post',
				url       : param.url      || '',
				dataType  : param.dataType || 'json',
				data      : param.data     ||  null,
				success   : (res) => {
					if (res.status === 0) {
						typeof resolve === 'function' && resolve(res.data,res.msg);
					} else if (res.status === 10) {
						//未登录跳转登录
						this.doLogin();
					}else {
						typeof reject === 'function' && reject(res.msg);
					}
					
				},
				fail     : (err) => {
					typeof reject === 'function' && reject(err.statusText);
				}
			})
		});
	}
	
	// 跳转登录
	doLogin(){
		console.log('123');
		window.location.href = '/login?redirect=' + encodeURIComponent(window.location.pathname);
	}
	// 获取URL参数
	getUrlParam(name){
		// param=123&param1=456
		let queryString = window.location.search.split('?')[1] || '',
			reg         = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
			result      = queryString.match(reg);
		return result ? decodeURIComponent(result[2]) : '/';
	}
	
	successTips(msg){
		alert(msg || '操作成功');
	}
	
	errorTips(msg){
		alert(msg || '哪里出错了');
	}
	
	setStorage(name,data){
		const dataType = typeof data;
		if (dataType === 'object' ) {
			window.localStorage.setItem(name,JSON.stringify(data));
		} else if(['string','number','boolean'].indexOf(dataType) >= 0){
			window.localStorage.setItem(name,data);
		}else {
			alert('不支持存储该数据类型')
		}
	}
	
	getStorage(name){
		let data = window.localStorage.getItem(name);
		if(data){
			return JSON.parse(data);
		}else{
			return '';
		}
		
	}
	
	removeStorage(name){
		window.localStorage.removeItem(name);
	}
	
	
}

export default Util;